<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChatMessageTable extends Migration
{
    /**
     * Run the migrations.
     * 聊天数据
     *
     * @return void
     */
    public function up()
    {
        //个人私聊消息 也有可能是文件
        Schema::create('chat_employee_message', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('receiver_id')->unsigned()->comment('接受者id号')->nullable();
            $table->foreign('receiver_id')->references('id')->on('employees')->onDelete('set null');
            $table->integer('sender_id')->unsigned()->comment('发送者id号')->nullable();
            $table->foreign('sender_id')->references('id')->on('employees')->onDelete('set null');
            $table->string('message')->comment('聊天内容')->nullable();
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->timestamps();
        });

        //群聊消息 也有可能是文件
        Schema::create('chat_group_message', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('chat_id')->unsigned()->comment('群聊号')->nullable();
            $table->foreign('chat_id')->references('id')->on('chat_group')->onDelete('set null');
            $table->integer('sender_id')->unsigned()->comment('聊天人')->nullable();
            $table->foreign('sender_id')->references('id')->on('employees')->onDelete('set null');
            $table->string('message')->comment('聊天内容')->nullable();
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->timestamps();
        });
        //收藏消息
        Schema::create('chat_favor_message', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('chat_message_id')->comment('');
            $table->unsignedInteger('employee_id')->comment('消息收藏人');
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('no action');
            $table->tinyInteger('type')->comment('收藏类型 1.私聊 2.群聊');
            $table->unique(['chat_message_id','employee_id','type'],'c_e');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('chat_favor_message');
        Schema::dropIfExists('chat_group_message');
        Schema::dropIfExists('chat_employee_message');
    }
}
